package gui;

import classes.Servidor;
import classes.Tarefa;
import classes.TarefaServidor;
import controlador.LoginControlador;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.GeralException;
import fachada.Fachada;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.apache.commons.mail.EmailException;
import rn.Email;
import rn.TarefaStatus;

/**
 *
 * @author Roberta
 */
public class TarefaCadastrar extends javax.swing.JFrame {

    DefaultListModel dlm = new DefaultListModel();
    DefaultListModel dlm2 = new DefaultListModel();

    public TarefaCadastrar() {
        initComponents();
        this.setLocationRelativeTo(null);
        carregarList();
        textDetalhe.setLineWrap(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btGrupo = new javax.swing.ButtonGroup();
        grupobtAlternar = new javax.swing.ButtonGroup();
        lbTarefa_id = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        textAtividade = new javax.swing.JTextField();
        textDetalhe = new javax.swing.JTextArea();
        textData = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        rdAlta = new javax.swing.JRadioButton();
        rdMedia = new javax.swing.JRadioButton();
        rdBaixa = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listServidor = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        listServidor2 = new javax.swing.JList();
        jPanel6 = new javax.swing.JPanel();
        btAdd = new javax.swing.JButton();
        btRetira = new javax.swing.JButton();
        btAddAll = new javax.swing.JButton();
        btRetiraAll = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        btIncluir = new javax.swing.JButton();
        btLimpar = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Tarefa");
        setBackground(new java.awt.Color(200, 200, 200));

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel4.setText("Detalhamento:");

        jLabel3.setText("Atividade:");

        jLabel6.setText("Data Final:");

        textAtividade.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                textAtividadeFocusLost(evt);
            }
        });

        textDetalhe.setColumns(20);
        textDetalhe.setFont(new java.awt.Font("Tahoma", 0, 13));
        textDetalhe.setRows(5);
        textDetalhe.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textDetalheKeyPressed(evt);
            }
        });

        jLabel1.setText("Servidor:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Prioridade:"));

        btGrupo.add(rdAlta);
        rdAlta.setText("Alta");

        btGrupo.add(rdMedia);
        rdMedia.setText("Media");

        btGrupo.add(rdBaixa);
        rdBaixa.setText("Baixa");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rdAlta)
                .addGap(18, 18, 18)
                .addComponent(rdMedia)
                .addGap(18, 18, 18)
                .addComponent(rdBaixa)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdAlta)
                    .addComponent(rdBaixa)
                    .addComponent(rdMedia))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel5.setText("*");

        jLabel7.setText("*");

        jLabel8.setText("*");

        jLabel9.setText("*");

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        listServidor.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listServidor.setInheritsPopupMenu(true);
        jScrollPane1.setViewportView(listServidor);

        listServidor2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane2.setViewportView(listServidor2);

        btAdd.setText(">");
        btAdd.setToolTipText("Adiciona o Servidor selecionado.");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btRetira.setText("<");
        btRetira.setToolTipText("Remove o Servidor selecionado.");
        btRetira.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRetiraActionPerformed(evt);
            }
        });

        btAddAll.setText(">>");
        btAddAll.setToolTipText("Adiciona todos os Servidores da lista.");
        btAddAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddAllActionPerformed(evt);
            }
        });

        btRetiraAll.setText("<<");
        btRetiraAll.setToolTipText("Remove todos os Servidores da lista.");
        btRetiraAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRetiraAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btRetiraAll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btAdd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btRetira, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btAddAll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btRetira)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAddAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btRetiraAll)
                .addGap(84, 84, 84))
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel2.setText("*");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(textData, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5))
                            .addComponent(textAtividade, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addGap(19, 19, 19))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(textDetalhe, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textAtividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(textDetalhe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(19, 19, 19)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel9)
                        .addContainerGap(23, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btIncluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/adicionar.png"))); // NOI18N
        btIncluir.setText("Incluir");
        btIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btIncluirActionPerformed(evt);
            }
        });
        btIncluir.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btIncluirKeyPressed(evt);
            }
        });

        btLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/limpar.png"))); // NOI18N
        btLimpar.setText("Limpar");
        btLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLimparActionPerformed(evt);
            }
        });
        btLimpar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btLimparKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(btIncluir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btLimpar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(btIncluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btLimpar)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jLabel10.setText("* Campos obrigatórios.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 381, Short.MAX_VALUE)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparActionPerformed
        limparTela();
        // limpartabela();
    }//GEN-LAST:event_btLimparActionPerformed

    private void btIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btIncluirActionPerformed
        try {
            Fachada fachada = new Fachada();
            Servidor s = new Servidor();
            Tarefa tarefa = new Tarefa();
            TarefaServidor ts = new TarefaServidor();
            String lc = LoginControlador.login; //Pega o login.
            s.setLogin(lc);
            fachada.pesquisarLogin(s);//Pesquisa o login.
            tarefa.setSolicitante(s);
            tarefa.setAtividade(textAtividade.getText().trim());
            tarefa.setDetalhe(textDetalhe.getText().trim());
            tarefa.setDataPrevista(textData.getDate());

            DateFormat formatdata = new SimpleDateFormat("dd/MM/yyyy");
            Date data = tarefa.getDataPrevista();
            String datA = (formatdata.format(data));
            int prioridade = 0;

            if (rdAlta.isSelected()) {
                prioridade = 3;//Se a prioridade for alta seta o número 3 no banco.
            }
            if (rdMedia.isSelected()) {
                prioridade = 2;//Se a prioridade for alta seta o número 2 no banco.
            }
            if (rdBaixa.isSelected()) {
                prioridade = 1;//Se a prioridade for alta seta o número 1 no banco.
            }
            tarefa.setPrioridade(prioridade);
            tarefa.setStatus(0);//zero é o status de tarefa não iniciada.
            s = new Servidor();
            tarefa = (fachada.consultarid(tarefa));//Pesquisa o último id inserido no banco.
            int id = tarefa.getTarefa_id() + 1;
            tarefa.setTarefa_id(id);

            ArrayList lista = new ArrayList();
            ArrayList<Servidor> listaServidores = fachada.listar(s);
            DefaultListModel modelol = (DefaultListModel) listServidor2.getModel();
            for (int l = 0; l < modelol.getSize(); l++) {
                String servAtual = modelol.getElementAt(l).toString();
                for (Servidor serv : listaServidores) {
                    if (serv.getNome().equals(servAtual)) {
                        lista.add(serv);
                    }
                }
            }
            ts.setTarefa_id(tarefa);
            ts.setServidor(lista);
            String emails = "";
            String nomes = "";
            String separador = "";

            for (int n = 0; n < lista.size(); n++) {
                if (lista.size() != 1) {
                    if (n + 1 == lista.size()) {
                        separador = "";
                    } else {
                        separador = ", ";
                    }
                }
                nomes += ts.getServidor().get(n).getNome() + separador;
                if (ts.getServidor().get(n).getEmail() == 1) {
                    emails += ts.getServidor().get(n).getEmailPessoal().toLowerCase().trim() + ",";
                }
                emails += ts.getServidor().get(n).getEmailProf().toLowerCase().trim() + separador;
            }
            fachada.inserirTarefa(tarefa, ts);

            int getPri = tarefa.getPrioridade();
            String setPri = null;
            switch (getPri) {
                case 3:
                    setPri = "Alta";
                    break;
                case 2:
                    setPri = "Média";
                    break;
                case 1:
                    setPri = "Baixa";
                    break;
            }

            for (int i = 0; i < lista.size(); i++) {
                Email.assunto = "Você recebeu uma nova tarefa de " + tarefa.getSolicitante().getNome();
                Email.conteudo = "Deverá ser concluída até: " + datA + "\n" + "Tarefa: " + tarefa.getAtividade()
                        + "." + "\n" + "Prioridade: " + setPri + "." + "\n" + "Detalhamento: " + tarefa.getDetalhe()
                        + "." + "\n" + "Responsáveis: " + nomes + ".";
            }
            Email.destino = emails;
            Email e = new Email();
            e.enviaEmail();
            limparTela();
            JOptionPane.showMessageDialog(null, "Os e-mails já foram enviados aos responsáveis!");
            this.dispose();
            TarefaStatus.alterarStatus();
        } catch (ConexaoException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + e);
        } catch (ConsultaException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível inserir o novo Evento!\n" + e);
        } catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
            this.repaint();
        } catch (EmailException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }//GEN-LAST:event_btIncluirActionPerformed

    private void textAtividadeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textAtividadeFocusLost
    }//GEN-LAST:event_textAtividadeFocusLost

    private void textDetalheKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textDetalheKeyPressed
        textDetalhe.setWrapStyleWord(true);
        textDetalhe.setLineWrap(true);
        /*try {
            jbInit();
            lbCaracteres.setText("Caracteres: " + String.valueOf(textDetalhe.getText().length()));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        }*/
    }//GEN-LAST:event_textDetalheKeyPressed

    private void btIncluirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btIncluirKeyPressed
        if (KeyEvent.getKeyText(evt.getKeyCode()).equals("Enter")) {
            btIncluirActionPerformed(null);
        }
    }//GEN-LAST:event_btIncluirKeyPressed

    private void btLimparKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btLimparKeyPressed
        if (KeyEvent.getKeyText(evt.getKeyCode()).equals("Enter")) {
            btLimparActionPerformed(null);
        }
    }//GEN-LAST:event_btLimparKeyPressed

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        if (listServidor.getSelectedValue() == null) {
            JOptionPane.showMessageDialog(null, "Selecione pelo menos um Servidor.");
        } else {
            for (int i = 0; i < listServidor.getSelectedValuesList().size(); i++) {
                dlm.addElement(listServidor.getSelectedValue());
            }
            listServidor2.setModel(dlm);
            int[] selecao = listServidor.getSelectedIndices();
            for (int i = selecao.length; i > 0; i--) {
                int j = listServidor.getSelectedIndex();
                ((DefaultListModel) listServidor.getModel()).remove(j);
            }
        }
    }//GEN-LAST:event_btAddActionPerformed

    private void btRetiraAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRetiraAllActionPerformed
        int indices = listServidor2.getModel().getSize() + 1;
        final int[] vetor = new int[indices];
        for (int i = 0; i < indices; i++) {
            vetor[i] = i;
        }
        listServidor2.setSelectedIndices(vetor);

        for (int i = 0; i < listServidor2.getSelectedValuesList().size(); i++) {
            dlm2.addElement(listServidor2.getModel().getElementAt(i));
        }
        listServidor.setModel(dlm2);

        int[] selecao = listServidor2.getSelectedIndices();
        for (int i = selecao.length; i > 0; i--) {
            ((DefaultListModel) listServidor2.getModel()).remove(0);
        }
    }//GEN-LAST:event_btRetiraAllActionPerformed

    private void btAddAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddAllActionPerformed
        int indices = listServidor.getModel().getSize() + 1;
        final int[] vetor = new int[indices];
        for (int i = 0; i < indices; i++) {
            vetor[i] = i;
        }
        listServidor.setSelectedIndices(vetor);

        for (int i = 0; i < listServidor.getSelectedValuesList().size(); i++) {
            dlm.addElement(listServidor.getModel().getElementAt(i));
        }
        listServidor2.setModel(dlm);

        int[] selecao = listServidor.getSelectedIndices();
        for (int i = selecao.length; i > 0; i--) {
            ((DefaultListModel) listServidor.getModel()).remove(0);
        }
    }//GEN-LAST:event_btAddAllActionPerformed

    private void btRetiraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRetiraActionPerformed
        if (listServidor2.getSelectedValue() == null) {
            JOptionPane.showMessageDialog(null, "Selecione pelo menos um Servidor.");
        } else {
            for (int i = 0; i < listServidor2.getSelectedValuesList().size(); i++) {
                int j = listServidor2.getSelectedIndex();
                dlm2.addElement(listServidor2.getSelectedValue());
            }
            listServidor.setModel(dlm2);

            int[] selecao = listServidor2.getSelectedIndices();
            for (int i = selecao.length; i > 0; i--) {
                int j = listServidor2.getSelectedIndex();
                ((DefaultListModel) listServidor2.getModel()).remove(j);
            }
        }
    }//GEN-LAST:event_btRetiraActionPerformed

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TarefaCadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TarefaCadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TarefaCadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TarefaCadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TarefaCadastrar().setVisible(true);
            }
        });
    }

    public void limparTela() {
        textDetalhe.setText("");
        textAtividade.setText("");
        textData.setDate(null);
    }

    private void jbInit() throws Exception {
        textDetalhe.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                txtArea_keyTyped(e);
            }
        });
    }

    private void txtArea_keyTyped(KeyEvent e) {
        //Número de caracteres que vai aceitar é 200.
        if (textDetalhe.getText().length() >= 200) {
            e.consume();
        } else {
            return;
        }
    }

    public final void carregarList() {
        Fachada f = new Fachada();
        Servidor s = new Servidor();
        DefaultListModel modelo = new DefaultListModel();
        ArrayList<Servidor> lista = new ArrayList<Servidor>();
        try {
            lista = f.listar(s);
            for (int i = 0; i < lista.size(); i++) {
                modelo.addElement(lista.get(i).getNome());
            }
            listServidor.setModel(modelo);
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível conectar-se ao banco de dados!\n" + ex);
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, "Ocorreu um erro ao tentar carregar a lista de Servidores.\n" + ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btAddAll;
    private javax.swing.ButtonGroup btGrupo;
    private javax.swing.JButton btIncluir;
    private javax.swing.JButton btLimpar;
    private javax.swing.JButton btRetira;
    private javax.swing.JButton btRetiraAll;
    private javax.swing.ButtonGroup grupobtAlternar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbTarefa_id;
    private javax.swing.JList listServidor;
    private javax.swing.JList listServidor2;
    public javax.swing.JRadioButton rdAlta;
    public javax.swing.JRadioButton rdBaixa;
    public javax.swing.JRadioButton rdMedia;
    public javax.swing.JTextField textAtividade;
    private com.toedter.calendar.JDateChooser textData;
    public javax.swing.JTextArea textDetalhe;
    // End of variables declaration//GEN-END:variables
}
